package nz.ac.massey.doi_information_extractor.tests;

import static org.junit.Assert.*;

import java.sql.SQLException;

import nz.ac.massey.doi_information_extractor.data.Author;
import nz.ac.massey.doi_information_extractor.data.AuthorList;
import nz.ac.massey.doi_information_extractor.data.Publication;
import nz.ac.massey.doi_information_extractor.database.DatabaseInterface;
import nz.ac.massey.doi_information_extractor.database.MockDatabase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MockDatabaseTest {

	DatabaseInterface database;

	@Before
	public void setUp(){
		database = new MockDatabase();
	}

	@After
	public void tearDown(){
		database = null;
	}

	@Test
	public void testReadWriteSingle() throws SQLException{
		Author author1 = new Author("Fname1", "Lname1", "organisation1", "department1", "Authortype1",  "userId1");
		Author author2 = new Author("Fname2", "Lname2", "organisation2", "department2", "Authortype2",  "userId2");
		Author author3 = new Author("Fname3", "Lname3", "organisation3", "department3", "Authortype3",  "userId3");

		AuthorList authorList = new AuthorList();
		authorList.add(author1);
		authorList.add(author2);
		authorList.add(author3);

		database.writeAuthor(author1);
		database.writeAuthor(author2);
		database.writeAuthor(author3);

		Publication publication1 = new Publication();
		publication1.setArticleTitle("ArticleTitle 1");
		publication1.setAuthors(authorList);
		publication1.setConfidential(true);
		publication1.setDate("date1");
		publication1.setDescription("description1 eargraergeargaergaergeargeargaergeargeargaergeargaergeargeargeagaergaergaergaergaergaergaergaergaergaergaergaergaerg");
		publication1.setDoi("doi1");
		publication1.setIssn("issn1");
		publication1.setPageNumbers("pagenumber1");
		publication1.setPublicationTitle("title1");
		publication1.setPublicationType("Journal");
		publication1.setPublisher("publisher1");
		publication1.setQualityAssured(true);
		publication1.setResearchOrProfCom(true);
		publication1.setUrl("url1");
		publication1.setVolumeNumber("1");
		publication1.setKeywords("Keywords1");
		publication1.setConferenceDates("conferenceDates1");
		publication1.setConferenceLocation("conferenceLocation1");
		publication1.setPublisherLocation("publisherLocation1");
		publication1.setEditors("editors1");
		publication1.setJournalNumber("journalNumber1");
		publication1.setEdition("edition1");

		try {
			database.write(publication1);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication readResult = database.read("doi1");

		assertTrue(publication1.equalsFull(readResult));
	}

	@Test
	public void testReadWriteMultiple() throws SQLException{
		AuthorList authorList = new AuthorList();
		authorList.add(new Author(1, "Fname1", "Lname1", "organisation1", "department1", "Authortype1", "userId1"));
		authorList.add(new Author(2, "Fname2", "Lname2", "organisation2", "department2", "Authortype2", "userId2"));
		authorList.add(new Author(3, "Fname3", "Lname3", "organisation3", "department3", "Authortype3", "userId3"));

		Publication publication1 = new Publication();
		publication1.setArticleTitle("ArticleTitle 1");
		publication1.setAuthors(authorList);
		publication1.setConfidential(true);
		publication1.setDate("date1");
		publication1.setDescription("description1 eargraergeargaergaergeargeargaergeargeargaergeargaergeargeargeagaergaergaergaergaergaergaergaergaergaergaergaergaerg");
		publication1.setDoi("doi1");
		publication1.setIssn("issn1");
		publication1.setPageNumbers("pagenumber1");
		publication1.setPublicationTitle("title1");
		publication1.setPublicationType("Journal");
		publication1.setPublisher("publisher1");
		publication1.setQualityAssured(true);
		publication1.setResearchOrProfCom(true);
		publication1.setUrl("url1");
		publication1.setVolumeNumber("1");
		publication1.setKeywords("Keywords1");
		publication1.setConferenceDates("conferenceDates1");
		publication1.setConferenceLocation("conferenceLocation1");
		publication1.setPublisherLocation("publisherLocation1");
		publication1.setEditors("editors1");
		publication1.setJournalNumber("journalNumber1");
		publication1.setEdition("edition1");


		try {
			database.write(publication1);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication2 = new Publication();
		publication2.setArticleTitle("ArticleTitle 2");
		publication2.setAuthors(authorList);
		publication2.setConfidential(true);
		publication2.setDate("date2");
		publication2.setDescription("description2");
		publication2.setDoi("doi2");
		publication2.setIssn("issn2");
		publication2.setPageNumbers("pagenumber2");
		publication2.setPublicationTitle("title2");
		publication2.setPublicationType("type2");
		publication2.setPublisher("publisher2");
		publication2.setQualityAssured(true);
		publication2.setResearchOrProfCom(true);
		publication2.setUrl("url2");
		publication2.setVolumeNumber("2");
		publication2.setKeywords("Keywords2");
		publication2.setConferenceDates("conferenceDates2");
		publication2.setConferenceLocation("conferenceLocation2");
		publication2.setPublisherLocation("publisherLocation2");
		publication2.setEditors("editors2");
		publication2.setJournalNumber("journalNumber2");
		publication2.setEdition("edition2");

		try {
			database.write(publication2);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication3 = new Publication();
		publication3.setArticleTitle("ArticleTitle 3");
		publication3.setAuthors(authorList);
		publication3.setConfidential(true);
		publication3.setDate("date3");
		publication3.setDescription("description3");
		publication3.setDoi("doi3");
		publication3.setIssn("issn3");
		publication3.setPageNumbers("pagenumber3");
		publication3.setPublicationTitle("title3");
		publication3.setPublicationType("type3");
		publication3.setPublisher("publisher3");
		publication3.setQualityAssured(true);
		publication3.setResearchOrProfCom(true);
		publication3.setUrl("url3");
		publication3.setVolumeNumber("3");
		publication3.setKeywords("Keywords3");
		publication3.setConferenceDates("conferenceDates3");
		publication3.setConferenceLocation("conferenceLocation3");
		publication3.setPublisherLocation("publisherLocation3");
		publication3.setEditors("editors1");
		publication3.setJournalNumber("journalNumber3");
		publication3.setEdition("edition3");

		try {
			database.write(publication3);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication4 = new Publication();
		publication4.setArticleTitle("ArticleTitle 4");
		publication4.setAuthors(authorList);
		publication4.setConfidential(true);
		publication4.setDate("date4");
		publication4.setDescription("description4");
		publication4.setDoi("doi4");
		publication4.setIssn("issn4");
		publication4.setPageNumbers("pagenumber4");
		publication4.setPublicationTitle("title4");
		publication4.setPublicationType("type4");
		publication4.setPublisher("publisher4");
		publication4.setQualityAssured(true);
		publication4.setResearchOrProfCom(true);
		publication4.setUrl("url4");
		publication4.setVolumeNumber("4");
		publication4.setKeywords("Keywords4");
		publication4.setConferenceDates("conferenceDates4");
		publication4.setConferenceLocation("conferenceLocation4");
		publication4.setPublisherLocation("publisherLocation4");
		publication4.setEditors("editors4");
		publication4.setJournalNumber("journalNumber4");
		publication4.setEdition("edition4");



		try {
			database.write(publication4);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication5 = new Publication();
		publication5.setArticleTitle("ArticleTitle 5");
		publication5.setAuthors(authorList);
		publication5.setConfidential(true);
		publication5.setDate("date5");
		publication5.setDescription("description5");
		publication5.setDoi("doi5");
		publication5.setIssn("issn5");
		publication5.setPageNumbers("pagenumber5");
		publication5.setPublicationTitle("title5");
		publication5.setPublicationType("type5");
		publication5.setPublisher("publisher5");
		publication5.setQualityAssured(true);
		publication5.setResearchOrProfCom(true);
		publication5.setUrl("url5");
		publication5.setVolumeNumber("5");
		publication5.setKeywords("Keywords5");
		publication5.setConferenceDates("conferenceDates5");
		publication5.setConferenceLocation("conferenceLocation5");
		publication5.setPublisherLocation("publisherLocation5");
		publication5.setEditors("editors5");
		publication5.setJournalNumber("journalNumber5");
		publication5.setEdition("edition5");

		try {
			database.write(publication5);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication readResult1 = database.read("doi1");
		Publication readResult2 = database.read("doi2");
		Publication readResult3 = database.read("doi3");
		Publication readResult4 = database.read("doi4");
		Publication readResult5 = database.read("doi5");

		assertTrue(readResult1.equalsFull(publication1));
		assertTrue(readResult2.equalsFull(publication2));
		assertTrue(readResult3.equalsFull(publication3));
		assertTrue(readResult4.equalsFull(publication4));
		assertTrue(readResult5.equalsFull(publication5));
	}

	@Test
	public void testGetDoiList() throws SQLException{
		AuthorList authorList = new AuthorList();
		authorList.add(new Author(1, "Fname1", "Lname1", "organisation1", "department1", "Authortype1", "userId1"));
		authorList.add(new Author(2, "Fname2", "Lname2", "organisation2", "department2", "Authortype2", "userId2"));
		authorList.add(new Author(3, "Fname3", "Lname3", "organisation3", "department3", "Authortype3", "userId3"));

		Publication publication1 = new Publication();
		publication1.setArticleTitle("ArticleTitle 1");
		publication1.setAuthors(authorList);
		publication1.setConfidential(true);
		publication1.setDate("date1");
		publication1.setDescription("description1 eargraergeargaergaergeargeargaergeargeargaergeargaergeargeargeagaergaergaergaergaergaergaergaergaergaergaergaergaerg");
		publication1.setDoi("doi1");
		publication1.setIssn("issn1");
		publication1.setPageNumbers("pagenumber1");
		publication1.setPublicationTitle("title1");
		publication1.setPublicationType("Journal");
		publication1.setPublisher("publisher1");
		publication1.setQualityAssured(true);
		publication1.setResearchOrProfCom(true);
		publication1.setUrl("url1");
		publication1.setVolumeNumber("1");
		publication1.setKeywords("Keywords1");
		publication1.setConferenceDates("conferenceDates1");
		publication1.setConferenceLocation("conferenceLocation1");
		publication1.setPublisherLocation("publisherLocation1");
		publication1.setEditors("editors1");
		publication1.setJournalNumber("journalNumber1");
		publication1.setEdition("edition1");


		try {
			database.write(publication1);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication2 = new Publication();
		publication2.setArticleTitle("ArticleTitle 2");
		publication2.setAuthors(authorList);
		publication2.setConfidential(true);
		publication2.setDate("date2");
		publication2.setDescription("description2");
		publication2.setDoi("doi2");
		publication2.setIssn("issn2");
		publication2.setPageNumbers("pagenumber2");
		publication2.setPublicationTitle("title2");
		publication2.setPublicationType("type2");
		publication2.setPublisher("publisher2");
		publication2.setQualityAssured(true);
		publication2.setResearchOrProfCom(true);
		publication2.setUrl("url2");
		publication2.setVolumeNumber("2");
		publication2.setKeywords("Keywords2");
		publication2.setConferenceDates("conferenceDates2");
		publication2.setConferenceLocation("conferenceLocation2");
		publication2.setPublisherLocation("publisherLocation2");
		publication2.setEditors("editors2");
		publication2.setJournalNumber("journalNumber2");
		publication2.setEdition("edition2");

		try {
			database.write(publication2);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication3 = new Publication();
		publication3.setArticleTitle("ArticleTitle 3");
		publication3.setAuthors(authorList);
		publication3.setConfidential(true);
		publication3.setDate("date3");
		publication3.setDescription("description3");
		publication3.setDoi("doi3");
		publication3.setIssn("issn3");
		publication3.setPageNumbers("pagenumber3");
		publication3.setPublicationTitle("title3");
		publication3.setPublicationType("type3");
		publication3.setPublisher("publisher3");
		publication3.setQualityAssured(true);
		publication3.setResearchOrProfCom(true);
		publication3.setUrl("url3");
		publication3.setVolumeNumber("3");
		publication3.setKeywords("Keywords3");
		publication3.setConferenceDates("conferenceDates3");
		publication3.setConferenceLocation("conferenceLocation3");
		publication3.setPublisherLocation("publisherLocation3");
		publication3.setEditors("editors1");
		publication3.setJournalNumber("journalNumber3");
		publication3.setEdition("edition3");

		try {
			database.write(publication3);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication4 = new Publication();
		publication4.setArticleTitle("ArticleTitle 4");
		publication4.setAuthors(authorList);
		publication4.setConfidential(true);
		publication4.setDate("date4");
		publication4.setDescription("description4");
		publication4.setDoi("doi4");
		publication4.setIssn("issn4");
		publication4.setPageNumbers("pagenumber4");
		publication4.setPublicationTitle("title4");
		publication4.setPublicationType("type4");
		publication4.setPublisher("publisher4");
		publication4.setQualityAssured(true);
		publication4.setResearchOrProfCom(true);
		publication4.setUrl("url4");
		publication4.setVolumeNumber("4");
		publication4.setKeywords("Keywords4");
		publication4.setConferenceDates("conferenceDates4");
		publication4.setConferenceLocation("conferenceLocation4");
		publication4.setPublisherLocation("publisherLocation4");
		publication4.setEditors("editors4");
		publication4.setJournalNumber("journalNumber4");
		publication4.setEdition("edition4");



		try {
			database.write(publication4);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication5 = new Publication();
		publication5.setArticleTitle("ArticleTitle 5");
		publication5.setAuthors(authorList);
		publication5.setConfidential(true);
		publication5.setDate("date5");
		publication5.setDescription("description5");
		publication5.setDoi("doi5");
		publication5.setIssn("issn5");
		publication5.setPageNumbers("pagenumber5");
		publication5.setPublicationTitle("title5");
		publication5.setPublicationType("type5");
		publication5.setPublisher("publisher5");
		publication5.setQualityAssured(true);
		publication5.setResearchOrProfCom(true);
		publication5.setUrl("url5");
		publication5.setVolumeNumber("5");
		publication5.setKeywords("Keywords5");
		publication5.setConferenceDates("conferenceDates5");
		publication5.setConferenceLocation("conferenceLocation5");
		publication5.setPublisherLocation("publisherLocation5");
		publication5.setEditors("editors5");
		publication5.setJournalNumber("journalNumber5");
		publication5.setEdition("edition5");

		try {
			database.write(publication5);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		String[] doiList = database.getDoiList();

		assertTrue(doiList[0].equals("doi1"));
		assertTrue(doiList[1].equals("doi2"));
		assertTrue(doiList[2].equals("doi3"));
		assertTrue(doiList[3].equals("doi4"));
		assertTrue(doiList[4].equals("doi5"));
	}

	@Test
	public void testDelete() throws SQLException{
		AuthorList authorList = new AuthorList();
		authorList.add(new Author(1, "Fname1", "Lname1", "organisation1", "department1", "Authortype1",  "userId1"));
		authorList.add(new Author(2, "Fname2", "Lname2", "organisation2", "department2", "Authortype2", "userId2"));
		authorList.add(new Author(3, "Fname3", "Lname3", "organisation3", "department3", "Authortype3", "userId3"));

		Publication publication1 = new Publication();
		publication1.setArticleTitle("ArticleTitle 1");
		publication1.setAuthors(authorList);
		publication1.setConfidential(true);
		publication1.setDate("date1");
		publication1.setDescription("description1 eargraergeargaergaergeargeargaergeargeargaergeargaergeargeargeagaergaergaergaergaergaergaergaergaergaergaergaergaerg");
		publication1.setDoi("doi1");
		publication1.setIssn("issn1");
		publication1.setPageNumbers("pagenumber1");
		publication1.setPublicationTitle("title1");
		publication1.setPublicationType("Journal");
		publication1.setPublisher("publisher1");
		publication1.setQualityAssured(true);
		publication1.setResearchOrProfCom(true);
		publication1.setUrl("url1");
		publication1.setVolumeNumber("1");
		publication1.setKeywords("Keywords1");
		publication1.setConferenceDates("conferenceDates1");
		publication1.setConferenceLocation("conferenceLocation1");
		publication1.setPublisherLocation("publisherLocation1");
		publication1.setEditors("editors1");
		publication1.setJournalNumber("journalNumber1");
		publication1.setEdition("edition1");


		try {
			database.write(publication1);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			database.delete("doi1");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication result = database.read("doi1");

		assertTrue(result == null);

	}

	@Test
	public void testOverwrite() throws SQLException{
		AuthorList authorList = new AuthorList();
		authorList.add(new Author(1, "Fname1", "Lname1", "organisation1", "department1", "Authortype1", "userId1"));
		authorList.add(new Author(2, "Fname2", "Lname2", "organisation2", "department2", "Authortype2", "userId2"));
		authorList.add(new Author(3, "Fname3", "Lname3", "organisation3", "department3", "Authortype3", "userId3"));

		Publication publication1 = new Publication();
		publication1.setArticleTitle("ArticleTitle 1");
		publication1.setAuthors(authorList);
		publication1.setConfidential(true);
		publication1.setDate("date1");
		publication1.setDescription("description1 eargraergeargaergaergeargeargaergeargeargaergeargaergeargeargeagaergaergaergaergaergaergaergaergaergaergaergaergaerg");
		publication1.setDoi("doi1");
		publication1.setIssn("issn1");
		publication1.setPageNumbers("pagenumber1");
		publication1.setPublicationTitle("title1");
		publication1.setPublicationType("Journal");
		publication1.setPublisher("publisher1");
		publication1.setQualityAssured(true);
		publication1.setResearchOrProfCom(true);
		publication1.setUrl("url1");
		publication1.setVolumeNumber("1");
		publication1.setKeywords("Keywords1");
		publication1.setConferenceDates("conferenceDates1");
		publication1.setConferenceLocation("conferenceLocation1");
		publication1.setPublisherLocation("publisherLocation1");
		publication1.setEditors("editors1");
		publication1.setJournalNumber("journalNumber1");
		publication1.setEdition("edition1");

		try {
			database.write(publication1);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication publication2 = database.read("doi1");

		publication2.setIssn("NEWISSN");

		try {
			database.write(publication2);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Publication result = database.read("doi1");

		assertTrue(result.getIssn().equals("NEWISSN"));

	}
}
